package com.unei.mq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by sunlei on 2015/7/16.
 */
public class Sender {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory;
        Connection connection=null;
        Session session;
        Destination destination;
        MessageProducer producer;
        connectionFactory=new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61617");
        try{
            connection=connectionFactory.createConnection();
            connection.start();

            session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
            destination=session.createQueue("FirstQueue");
            producer=session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            sendMessage(session,producer);

//            Thread.sleep(10000000L);
        }catch(Exception e){

        }finally{
            if(connection!=null)
                connection.close();
        }
    }

    public static void sendMessage(Session session,MessageProducer producer) throws Exception{
        for(int i=1;i<1000;i++){
            TextMessage message=session.createTextMessage("message:"+i);
            producer.send(message);
            session.commit();
            Thread.sleep(1000L);
        }
    }
}
